package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    /*



    @Schema(description = "房间是否被删除")
    private Boolean isDelete;

    @Schema(description = "房间是否已入住")
    private Boolean isCheckIn;
     */
    //图片
    @Autowired
   private GraphInfoMapper graphInfoMapper;

    //属性
   @Autowired
   private AttrValueMapper attrValueMapper;

    //配套信息列表
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    //标签信息列表
    @Autowired
    private LabelInfoMapper labelInfoMapper;

    //支付方式列表
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    //杂费列表
    @Autowired
    private FeeValueMapper feeValueMapper;

    //租期列表
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Override
    public void queryRoomItemVoOrPage(Page<RoomItemVo> page, @Param("queryVo") RoomQueryVo queryVo) {
        roomInfoMapper.queryRoomItemVoOrPage(page, queryVo);
    }


    @Override
    public RoomDetailVo queryRoomDetailVoById(Long id, Long userId) {
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        if (roomInfo == null) {
            return null;
        }
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        ApartmentItemVo apartmentItemVo = apartmentInfoMapper.ApartmentItemVo(id);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);

        List<GraphVo> graphVoList = graphInfoMapper.queryGraphById(id);
        roomDetailVo.setGraphVoList(graphVoList);

        List<AttrValueVo> attrValueVoList = attrValueMapper.queryAttrById(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        List<PaymentType> paymentTypeList = facilityInfoMapper.queryfFacilityById(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        List<FeeValueVo> feeValueVoList = feeValueMapper.queryFeeById(id);
        roomDetailVo.setFeeValueVoList(feeValueVoList);

        List<LeaseTerm> leaseTermList = leaseTermMapper.queryLeaseTermId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);


        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, roomInfo.getId());
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        Long singedCount = leaseAgreementMapper.selectCount(leaseAgreementLambdaQueryWrapper);

        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        roomDetailVo.setIsCheckIn(singedCount > 0);

        //保存浏览历史
        browsingHistoryService.saveHistory(userId, id);

        return roomDetailVo;
    }

    @Autowired
    private BrowsingHistoryService browsingHistoryService;

    @Override
    public void queryRoomItemVoById(Page<RoomItemVo> page, Long id) {
        page = roomInfoMapper.queryRoomItemVoById(page, id);
    }
}




